home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / overview / dtscpluslibrary / sources / randomtest.cp < prev    next >
Encoding:
Text File  |  2000-06-23  |  6.6 KB  |  247 lines

  1. /*
  2.     File:        RandomTest.cp
  3.  
  4.     Contains:    
  5.  
  6.     Written by: TRandom is a stackbased utility class for random number generation.
  7.                   TRandomTest.cp contains the test functions for the TRandom class.
  8.  
  9.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 8/18/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. // HEADERS
  25. #ifndef _RANDOM_
  26. #include "Random.h"
  27. #endif
  28.  
  29. // GLOBALS
  30. void CalcTime(const char* string);                // calculate time function
  31. unsigned long timeStart;                        // global timing
  32.  
  33.  
  34. // Test the TRandom class inside out.
  35.  
  36. void main(void)
  37. {
  38.     const short cARRAYSIZE = 80;                // 200 is a nice size
  39.     const long cBIGNUM = 1000;                    //cBIGNUM= 1000000 takes about 40 sec
  40.     unsigned short val;
  41.     long x;
  42.     unsigned short array1[cARRAYSIZE],
  43.      array2[cARRAYSIZE];
  44.  
  45.     InitGraf((Ptr) & qd.thePort);                // Initialize grafport!
  46.  
  47.     //    First initial quick test of the toolbox value
  48.     qd.randSeed = TickCount();
  49.     long xx = Random();
  50.     cerr << "Normal Toolbox Random     = " << xx << '\n';
  51.  
  52.  
  53.     //    1. Create an instance of each TRandom class
  54.     TRandom aRandom;
  55.     val = aRandom.Next();
  56.     cerr << "Toolbox aRandom.Next    = " << val << "\n";
  57.  
  58.     aRandom.SetRandomGenerator(TRandom::kSHUFFLE);
  59.     val = aRandom.Next();
  60.     cerr << "Shuffle aRandom.Next    = " << val << '\n';
  61.  
  62.     aRandom.SetRandomGenerator(TRandom::kPM);
  63.     val = aRandom.Next();
  64.     cerr << "PM aRandom.Next            = " << val << '\n';
  65.  
  66.     //     2. Efficiency test, docBIGNUMtimes random numbers and return the time
  67.     cerr << "Doing " << cBIGNUM << " random numbers of each class...this will take some time\n";
  68.  
  69.     // Toolbox Random    
  70.     aRandom.SetRandomGenerator(TRandom::kMACOS);
  71.     GetDateTime(&timeStart);
  72.     for (x = 0; x <= cBIGNUM; x++)
  73.     {
  74.         val = aRandom.Next();
  75. #if DEBUG
  76.         cerr << "Toolbox Random " << x << " = " << val << '\n';
  77. #endif
  78.  
  79.     }
  80.     CalcTime("Toolbox");
  81.  
  82.     // Shuffle Random    
  83.     aRandom.SetRandomGenerator(TRandom::kSHUFFLE);
  84.     GetDateTime(&timeStart);
  85.     for (x = 0; x <= cBIGNUM; x++)
  86.     {
  87.         val = aRandom.Next();
  88. #if DEBUG
  89.         cerr << "Shuffle Random " << x << " = " << val << '\n';
  90. #endif
  91.  
  92.     }
  93.     CalcTime("Shuffle");
  94.  
  95.     // PM Random
  96.     aRandom.SetRandomGenerator(TRandom::kPM);
  97.     GetDateTime(&timeStart);
  98.     for (x = 0; x <= cBIGNUM; x++)
  99.     {
  100.         val = aRandom.Next();
  101. #if DEBUG
  102.         cerr << "PM Random " << x << " = " << val << '\n';
  103. #endif
  104.  
  105.     }
  106.     CalcTime("PM");
  107.  
  108.     //    3. Performance test, create twice the random numbers with the same
  109.     //    original seed, and compare the results, signal if they differ
  110.  
  111.     long definedSeed;
  112.  
  113.     cerr << "Performance test, create from same seed twice and compare...\n";
  114.  
  115.     //    MACOS Random Performance test
  116.     cerr << "MacOS Random...";
  117.     aRandom.SetRandomGenerator(TRandom::kMACOS);
  118.  
  119.     aRandom.ShuffleSeed();                        // get new seed
  120.     definedSeed = aRandom.GetSeedValue();        // store it
  121.  
  122.     aRandom.SetSeedValue(definedSeed);            // back to first seed
  123.  
  124.     for (x = 0; x < cARRAYSIZE; x++)            // fill array1
  125.         array1[x] = aRandom.Next();
  126.  
  127.     aRandom.SetSeedValue(definedSeed);            // back to first seed
  128.     for (x = 0; x < cARRAYSIZE; x++)            // fill array2
  129.         array2[x] = aRandom.Next();
  130.  
  131.     for (x = 0; x < cARRAYSIZE; x++)
  132.     {                                            // compare
  133.         if (array1[x] != array2[x])
  134.         {
  135.             cerr << "problems with Toolbox values....\n";
  136.             cerr << "x = " << x << ",array1 = " << array1[x] << ", array2 = " << array2[x] << '\n';
  137.         }
  138.         else
  139.             cerr << ".";
  140.     }
  141.     cerr << "\n";
  142.  
  143.     //    Shuffle Random Performance test
  144.     cerr << "Shuffle Random...";
  145.     TRandom rand1(TRandom::kSHUFFLE);
  146.     TRandom rand2(TRandom::kSHUFFLE);
  147.  
  148.     aRandom.ShuffleSeed();                        // get new seed
  149.     definedSeed = aRandom.GetSeedValue();        // store it
  150.  
  151.     rand1.SetSeedValue(definedSeed);            // back to first seed
  152.  
  153.     for (x = 0; x < cARRAYSIZE; x++)            // fill array1
  154.         array1[x] = rand1.Next();
  155.  
  156.     rand2.SetSeedValue(definedSeed);            // back to first seed
  157.     for (x = 0; x < cARRAYSIZE; x++)            // fill array2
  158.         array2[x] = rand2.Next();
  159.  
  160.     for (x = 0; x < cARRAYSIZE; x++)
  161.     {                                            // compare
  162.         if (array1[x] != array2[x])
  163.         {
  164.             cerr << "problems with Quick values....\n";
  165.             cerr << "x = " << x << ",array1 = " << array1[x] << ", array2 = " << array2[x] << '\n';
  166.         }
  167.         else
  168.             cerr << ".";
  169.     }
  170.     cerr << "\n";
  171.  
  172.     //    Park-Miller
  173.     cerr << "PM Random...";
  174.     aRandom.SetRandomGenerator(TRandom::kPM);
  175.  
  176.     aRandom.ShuffleSeed();                        // get new seed
  177.     definedSeed = aRandom.GetSeedValue();        // store it
  178.  
  179.     aRandom.SetSeedValue(definedSeed);            // back to first seed
  180.  
  181.     for (x = 0; x < cARRAYSIZE; x++)            // fill array1
  182.         array1[x] = aRandom.Next();
  183.  
  184.     aRandom.SetSeedValue(definedSeed);            // back to first seed
  185.  
  186.     for (x = 0; x < cARRAYSIZE; x++)            // fill array2
  187.         array2[x] = aRandom.Next();
  188.  
  189.     for (x = 0; x < cARRAYSIZE; x++)
  190.     {                                            // compare
  191.         if (array1[x] != array2[x])
  192.         {
  193.             cerr << "problems with PM values....\n";
  194.             cerr << "x = " << x << ",array1 = " << array1[x] << ", array2 = " << array2[x] << '\n';
  195.         }
  196.         else
  197.             cerr << ".";
  198.     }
  199.     cerr << "\n";
  200.  
  201.     //    4. Test mutator class reference passing (syntax check)
  202.     aRandom.SetRandomGenerator(TRandom::kMACOS).ShuffleSeed();
  203.  
  204.     //    5. Test of copy constructor
  205.     cerr << "\nTest of copy constructor\n";
  206.  
  207.     TRandom bRandom(aRandom);
  208.     cerr << "Seed from aRandom = " << aRandom.GetSeedValue() << "\n";
  209.     cerr << "Seed from bRandom = " << bRandom.GetSeedValue() << "\n";
  210.     cerr << "Rand val from aRandom = " << aRandom.Next() << "\n";
  211.     cerr << "Rand val from bRandom = " << bRandom.Next() << "\n";
  212.  
  213.  
  214.     //     6. Test of assignment operator overload
  215.     cerr << "\nTest of assignment operator overload\n";
  216.  
  217.     TRandom cRandom = aRandom;
  218.     cerr << "Seed from aRandom = " << aRandom.GetSeedValue() << "\n";
  219.     cerr << "Seed from cRandom = " << cRandom.GetSeedValue() << "\n";
  220.     cerr << "Rand val from aRandom = " << aRandom.Next() << "\n";
  221.     cerr << "Rand val from cRandom = " << cRandom.Next() << "\n";
  222.  
  223.     cerr << "The end of the TRandom Test!\n";
  224. }
  225.  
  226.  
  227. void CalcTime(const char* string)
  228. // Calculate Time.
  229. {
  230.     unsigned long totalTime, timeEnd;
  231.  
  232.     ::GetDateTime(&timeEnd);
  233.     totalTime = timeEnd - timeStart;
  234.     cerr << "Time for \t" << string << "\t= " << totalTime << '\n';
  235. }
  236.  
  237.  
  238. // _________________________________________________________________________________________________________ //
  239.  
  240.  
  241. /*    Change History (most recent last):
  242.   No        Init.    Date        Comment
  243.   1            khs        6/2/92        New file
  244.   2            khs        1/7/93        Cleanup
  245. */
  246.  
  247.